package
{
	import com.usfuture.Canvas;
	import com.usfuture.ICallRequest;
	import com.usfuture.chart.techanly.AbstractTechProperty;
	import com.usfuture.chart.techanly.line.LineChart;
	import com.usfuture.customComponent.ButtonStyle1;
	import com.usfuture.event.BusyMaskEvent;
	import com.usfuture.ins.view.InsContainer;
	import com.usfuture.techanly.view.TechnicalAnalysisContainer;
	import com.usfuture.util.NumberFormat;
	
	import fl.events.ComponentEvent;
	
	import flash.display.Bitmap;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.StatusEvent;
	import flash.events.TimerEvent;
	import flash.external.ExternalInterface;
	import flash.geom.Rectangle;
	import flash.utils.Timer;
	
	
	public class future extends Sprite
	{
		public static var startTime:String;
		public static var endTime:String;
		public static var minuteShow:Boolean;
		public static var hourOffset:int;
		public static var restHour:int;
		public static var interval:uint;
//		private var insBtn:ButtonStyle1 = new ButtonStyle1('走势图');
//		private var lineBtn:ButtonStyle1 = new ButtonStyle1('技术分析');
		private var insContainer:InsContainer;
		private var tacontainer:TechnicalAnalysisContainer;
		private var callRequest:ICallRequest;
		private var isReady:Boolean;
		
		[Embed(source="/assets/busy.swf")]
		private var busy:Class; 
		private var busyMask:Sprite;
		private var liveTimer:Timer;
		public function future()
		{
			stage.scaleMode = StageScaleMode.NO_SCALE;
			//获得参数
			startTime = root.loaderInfo.parameters['startTime'];
			endTime = root.loaderInfo.parameters['endTime'];
			minuteShow =Boolean(root.loaderInfo.parameters['minuteShow']=='true');
			hourOffset = root.loaderInfo.parameters['hourOffset'];
			restHour = root.loaderInfo.parameters['restHour']?root.loaderInfo.parameters['restHour']:-1;
			interval = root.loaderInfo.parameters['interval'];
			NumberFormat.precision = root.loaderInfo.parameters['precision'];
			AbstractTechProperty.rightWidthX = root.loaderInfo.parameters['lineMarginRight']?root.loaderInfo.parameters['lineMarginRight']:60;
			stage.align = StageAlign.TOP_LEFT;
			updateDisplayList(stage.stageWidth,stage.stageHeight); 
			//初始化走势容器
//			insContainer = new InsContainer(stage.stageWidth - 10,stage.stageHeight - 10);
//			insContainer.x = 10;
////			insContainer.height = 0;
//			this.addChild(insContainer);
//			callRequest = insContainer;
			//初始化技术容器
			tacontainer = new TechnicalAnalysisContainer(stage.stageWidth - 10,stage.stageHeight - 10);
			tacontainer.x = 10;
			tacontainer.y = 10;
			//tacontainer.y = insContainer.height+10;
			this.addChild(tacontainer); 
			callRequest = tacontainer;
			
			//加载忙碌层
			var mc:MovieClip = new busy() as MovieClip;
			mc.width = 32;
			mc.height = 32;
			mc.x = stage.stageWidth/2 - mc.width;
			mc.y = stage.stageHeight/2 - mc.height;
			busyMask = new Canvas(this.width,this.height,0xFEFDE9,0.5);
			busyMask.visible = false;
			busyMask.addChild(mc);
			addChild(busyMask);
			this.addEventListener(BusyMaskEvent.SHOW,busyMaskEventHandle);
			this.addEventListener(BusyMaskEvent.HIDE,busyMaskEventHandle);
			
			if(!ExternalInterface.available)
				return;
			var readyTimer:Timer = new Timer(500, 0);
			readyTimer.addEventListener(TimerEvent.TIMER, timerHandler);
			readyTimer.start();
			
			
		}
		
		private function timerHandler(event:TimerEvent):void {
			isReady = ExternalInterface.call("isReady");
			if (isReady) {
				Timer(event.target).stop();
				callRequest.initCall();
				liveTimer = new Timer(interval*1000, 0);
				liveTimer.addEventListener(TimerEvent.TIMER, liveTimeHandler);
				liveTimer.start();
			}
		}
		
		/**
		 * 每秒请求服务器
		 * @param event
		 */
		private function liveTimeHandler(event:TimerEvent):void {
			callRequest.liveCall();
		}
		
		private function busyMaskEventHandle(event:BusyMaskEvent):void{
			switch(event.type){
				case BusyMaskEvent.SHOW:
					busyMask.visible=true; 
					break;
				case BusyMaskEvent.HIDE:
					busyMask.visible=false;
					break;
			}
			
		}
		

			
		
		public function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number) :void{
//			this.graphics.beginFill(0xFEFDE9,1);
			this.graphics.beginFill(0xFFFFFF,1);
			this.graphics.drawRect(0,0,unscaledWidth,unscaledHeight);
			this.graphics.endFill();
		}
	}
}